package com.service.scheduling.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.util.ObjectUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * <dl>
 * <dt>platform</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2018</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019-04-14</dd>
 * </dl>
 */
@Slf4j
public class DateUtil {

    public static String pattern_ymd = "yyyy-MM-dd";

    public static long getNowSec() {
        return System.currentTimeMillis() / 1000;
    }

    public static long getSec(Date date) {
        return date.getTime() / 1000;
    }

    public static Date getOnlyDay(Date date) {
        Date date1 = null;
        try {
            date1 = org.apache.commons.lang3.time.DateUtils.parseDate(DateFormatUtils.format(date, "yyyy-MM-dd"), "yyyy-MM-dd");
        } catch (ParseException e) {
            e.printStackTrace();
            log.error("日期格式化异常");
        }
        return date1;
    }

    public static Date getDayOfStart(Date date){
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);
        dateCalendar.set(Calendar.HOUR_OF_DAY, 0);
        return setDateZero2Date(dateCalendar);
    }

    public static Date getDayOfEnd(Date date) {
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);
        dateCalendar.set(Calendar.HOUR_OF_DAY, 24);
        return setDateZero2Date(dateCalendar);
    }

    /**
     * LocalDate转Date
     * @param localDate
     * @return
     */
    public static Date getDayOfStart(LocalDate localDate) {
        if (null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    private static Date setDateZero2Date(Calendar dateCalendar) {
        dateCalendar.set(Calendar.MINUTE, 0);
        dateCalendar.set(Calendar.SECOND, 0);
        dateCalendar.set(Calendar.MILLISECOND, 0);
        return dateCalendar.getTime();
    }

    public static Long getValidTime(Date createTime, Integer overdueTime) {
        Date overdueDate = DateUtils.addMinutes(createTime, overdueTime);
        long lastTime = overdueDate.getTime() - System.currentTimeMillis();
        return lastTime > 0 ? lastTime : 0;
    }

    public static boolean isWeekend(Date someday){
        Calendar cal = Calendar.getInstance();
        cal.setTime(someday);
        return cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
    }

    /**
     * 计算两个日期之间相差的天数
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate,Date bdate) throws ParseException
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        smdate=sdf.parse(sdf.format(smdate));
        bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 字符串的日期格式的计算
     */
    public static int daysBetween(String smdate,String bdate) throws ParseException{
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 当前时间向推几小时
     * @param ihour
     * @return
     */
    public static Date getBeforeHourTime(int ihour) {
        Calendar calendar = Calendar.getInstance();//Calendar.HOUR_OF_DAY 改一下就是分钟 、天 、月
                calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String str = "";
        //calendar.getTime()为date型
        // str = df.format(calendar.getTime()); <br>　　
        // return str;}
        return calendar.getTime();
    }

    public static Date nDaysAfterDate(Integer daysNum){
        if(ObjectUtils.isEmpty(daysNum) || daysNum == 0){
            daysNum = 1;
        }
        Calendar calendar2 = Calendar.getInstance();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        calendar2.add(Calendar.DATE, daysNum);
        String three_days_after = sdf1.format(calendar2.getTime());
        String nDaysAfterStr = three_days_after + " 00:00:00";
        System.out.println(nDaysAfterStr);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date returnDate = null;
        try {
            returnDate = sdf2.parse(nDaysAfterStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return returnDate;
    }

    /**
     * 将时间挪到下午15:00
     * @param date
     * @return
     */
    public static Date changeDateTo3point(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 将时分秒,毫秒域清零
        calendar.set(Calendar.HOUR_OF_DAY, 15);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);
        return calendar.getTime();
    }

    public static void main(String[] args) {
        System.out.println(nDaysAfterDate(1));
    }
}
